// 入口文件
import Vue from 'vue'
// 1.1 导入路由的包
import VueRouter from 'vue-router'
// 1.2 安装路由
Vue.use(VueRouter)

import Vuex from 'vuex'
Vue.use(Vuex)




// 导入格式化时间的插件
import moment from 'moment'
// 定义全局的过滤器
Vue.filter('dateFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
  return moment(dataStr).format(pattern)
})

// 2.1 导入 vue-resource
import VueResource from 'vue-resource'
// 2.2 安装 vue-resource
Vue.use(VueResource)
// 设置请求的根路径
//Vue.http.options.root = 'http://vue.studyit.io';
// 全局设置 post 时候表单数据格式组织形式   application/x-www-form-urlencoded
Vue.http.options.emulateJSON = true;


// 导入 MUI 的样式
import './lib/mui/css/mui.min.css'
// 导入扩展图标样式
import './lib/mui/css/icons-extra.css'


// 按需导入 Mint-UI 中的组件   
/* import { Header, Swipe, SwipeItem, Button, Lazyload } from 'mint-ui'
Vue.component(Header.name, Header)
Vue.component(Swipe.name, Swipe)
Vue.component(SwipeItem.name, SwipeItem)
Vue.component(Button.name, Button)
Vue.use(Lazyload); */
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'


// 安装 图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview, {
  mainClass: 'pswp--minimal--dark',
  barsSize: {top: 0, bottom: 0},
  captionEl: false,
  fullscreenEl: false,
  shareEl: false,
  bgOpacity: 0.85,
  tapToClose: true,
  tapToToggleControls: false})



// 1.3 导入自己的 router.js 路由模块
import router from './router.js'


// 导入 App 根组件
import app from './App.vue'

import './lib/css/golbal.css'

var car = JSON.parse(localStorage.getItem('car') ||'[]' )
var store = new Vuex.Store({
	state:{
		car:car
	},
	mutations:{
		addToCar(state,goodsinfo){
			//点击加入购物车把信息加入到stroe
			/*
			 如果已经存在循环只更新数量，否则push到数组中去*/
			var flag = false;
			state.car.some(item => {
				if(item.id == goodsinfo.id){
					item.count += parseInt(goodsinfo.count)
					flag = true
					return true
				}
				
			})
			if(!flag){
				state.car.push(goodsinfo)
			}
			localStorage.setItem("car",JSON.stringify(state.car))
		},
		//更新购物车的数量
		updateGoodsInfo(state,goodsinfo){
				state.car.some(item => {
					if(item.id == goodsinfo.id){
						item.count = parseInt(goodsinfo.count)
						return true
					}
				})
				localStorage.setItem("car",JSON.stringify(state.car))
		},
		removeGoodsCar(state,id){
				state.car.some(item => {
					if(item.id == id){
						state.car.splice(id,1);	
						return true
					}
				})
				localStorage.setItem("car",JSON.stringify(state.car))
		}
		,updateGoodsSelect(state,goodsinfo){
			 var c ="";
				state.car.some(item => {
					if(item.id == goodsinfo.id){
						item.selected = goodsinfo.selected
					}
					
				})
				localStorage.setItem("car",JSON.stringify(state.car))
			
		}
	},
	getters:{
		getAllCount(state){
			  var c = 0;
				state.car.forEach(item=> {
					c += item.count
				})
				return c;
		},
		getGoodsCount(state){
			var o = {}
			state.car.forEach(item=> {
					o[item.id] = item.count
			})
			console.log(o);
			return o;
		}
		,getGoodsSelect(state){
			var o = {}
			state.car.forEach(item=> {
					o[item.id] = item.selected
			})
			return o;
		}
		,getGoodsValue(state){
			var o = {
				count: 0,
				amount: 0
			}
			state.car.forEach(item=> {
					if(item.selected){
					 		o.amount += item.count*item.price;
					 		o.count += item.count;
					}
			})
			return o;
		}
		
	}
})
	


var vm = new Vue({
  el: '#app',
  render: c => c(app),
  router, // 1.4 挂载路由对象到 VM 实例上
  store
})